package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.TravelInItemModels;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lanouhn on 16/9/8.
 */
public class TravelInDayContentAdapter extends RecyclerView.Adapter<TravelInDayContentAdapter.InDayContentHolder> {
    private Context context;
    private TravelInItemModels.TripDaysBean.NodesBean travelInItemModles;
    private List<TravelInItemModels.NotesLikesCommentsBean> notesLikesCommentsBeen;


    public TravelInDayContentAdapter(Context context, TravelInItemModels.TripDaysBean.NodesBean travelInItemModles, List<TravelInItemModels.NotesLikesCommentsBean> notesLikesCommentsBeen) {
        this.context = context;
        this.travelInItemModles = travelInItemModles;
        this.notesLikesCommentsBeen = notesLikesCommentsBeen;
    }

    @Override
    public InDayContentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_travel_indaycontent_item, null);
        InDayContentHolder holder = new InDayContentHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(InDayContentHolder holder, int position) {

        int id = travelInItemModles.getNotes().get(position).getId();


        for (int i = 0; i < notesLikesCommentsBeen.size(); i++) {
            int isLikeId = notesLikesCommentsBeen.get(i).getId();
            if (id == isLikeId) {
                holder.commentsCount.setText(notesLikesCommentsBeen.get(i).getComments_count() + "");
                holder.likesCount.setText(notesLikesCommentsBeen.get(i).getLikes_count() + "");
            }
        }

        holder.commentB.setVisibility(View.VISIBLE);
        holder.like.setVisibility(View.VISIBLE);
        if (travelInItemModles.getNotes().get(position).getPhoto() == null) {
            holder.description.setText(travelInItemModles.getNotes().get(position).getDescription());
            holder.ivUrl.setVisibility(View.GONE);
            holder.image_description.setVisibility(View.GONE);
            holder.iv_position.setVisibility(View.VISIBLE);
            holder.entryName.setText(travelInItemModles.getEntry_name() + "");

        } else {
            if (travelInItemModles.getNotes().get(position).getDescription() == null) {
                holder.image_description.setVisibility(View.GONE);
                holder.description.setVisibility(View.GONE);
                holder.ivUrl.setVisibility(View.VISIBLE);
                Picasso.with(context).load(travelInItemModles.getNotes().get(position).getPhoto().getUrl()).into(holder.ivUrl);
                holder.iv_position.setVisibility(View.VISIBLE);
                holder.entryName.setText(travelInItemModles.getEntry_name() + "");
            } else {
                holder.description.setVisibility(View.GONE);
                holder.image_description.setVisibility(View.VISIBLE);
                holder.image_description.setText(travelInItemModles.getNotes().get(position).getDescription());
                holder.ivUrl.setVisibility(View.VISIBLE);
                Picasso.with(context).load(travelInItemModles.getNotes().get(position).getPhoto().getUrl()).into(holder.ivUrl);
            }

        }
        if (travelInItemModles.getEntry_name() == null) {
            holder.iv_position.setVisibility(View.GONE);
            holder.entryName.setVisibility(View.GONE);
        } else {
            holder.iv_position.setVisibility(View.VISIBLE);
            holder.entryName.setText(travelInItemModles.getEntry_name() + "");
        }

    }

    @Override
    public int getItemCount() {
        return travelInItemModles.getNotes().size();
    }

    public class InDayContentHolder extends RecyclerView.ViewHolder {
        private RelativeLayout rel1;
        private ImageView ivUrl;
        private TextView description;
        private RelativeLayout rel_bottom;
        private ImageView iv_position;
        private TextView entryName;
        private TextView commentsCount;
        private ImageView commentB;
        private ImageView like;
        private TextView likesCount;
        private TextView image_description;

        public InDayContentHolder(View itemView) {
            super(itemView);
            rel1 = (RelativeLayout) itemView.findViewById(R.id.rel1);
            ivUrl = (ImageView) itemView.findViewById(R.id.iv_url);
            description = (TextView) itemView.findViewById(R.id.description);

            rel_bottom = (RelativeLayout) itemView.findViewById(R.id.rel_bottom);
            iv_position = (ImageView) itemView.findViewById(R.id.iv_position);
            entryName = (TextView) itemView.findViewById(R.id.position_entryName);


            commentsCount = (TextView) itemView.findViewById(R.id.comments_count);
            commentB = (ImageView) itemView.findViewById(R.id.comment_b);
            like = (ImageView) itemView.findViewById(R.id.like);
            likesCount = (TextView) itemView.findViewById(R.id.likes_count);
            image_description = (TextView) itemView.findViewById(R.id.image_description);
        }
    }
}
